/*
 * Block comments with details of changes
 */
public without sharing class WorkOrderAttacmentUploadController
{
    public String msg {get;set;}//附件名称
    public String rename {get;set;}//附件重命名
    public String woId {get;set;}//工单id 
    public Boolean isSuccess {get;set;}//是否已经有附件了
    public Workorder__c w;//工单
    public Integer rptN {get;set;}
    public Boolean isErr {get;private set;}
    public WorkOrderAttacmentUploadController(ApexPages.StandardController con)
    {
        woId = System.currentPageReference().getParameters().get('id');
        w = [select Id,IMEINumber__c,ReportNumber__c,Case__r.Status from Workorder__c where Id =: woId];
        isErr = false;
        rptN = 0;
    }
    public void init(){

      try {
          if(String.isBlank(w.IMEINumber__c))throw new DmlException('安装工单的机身号不能为空!');
        
        List<Attachment>  attList = [select Id,Name from Attachment where ParentId = :woId and Name like '%安装%'];
        rptN = attList != null? attList.size() : 0;
      }
      catch (Exception e) {
          
          isErr = true;
          ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()));
      }
      
      
      //if(rptN > 0)save();

    }
    public void save(){
        msg = '';
        SavePoint sp = Database.setSavePoint();
    	try{
           String s = System.currentPageReference().getParameters().get('fileValue');
           Blob body = s != null ?EncodingUtil.base64Decode(s.substring(s.indexOf(',')+1) ) : null;
           String format = System.currentPageReference().getParameters().get('fileFormat');
           System.debug('***'+rptN +'-->' + s);

           if((s == null || s == '') && rptN == 0){
                msg = '请将上传文件名称重命名为“仪器型号+安装报告”的文件名，如：“IS1200安装报告”的命名方式。若格式命名错误，会导致上传无效！';
                return ;
            }
           String attName = null;
            if(String.isBlank(rename) == false){
                attName = rename +'.'+ format;
            }else{
                attName = System.currentPageReference().getParameters().get('fileName') +'.'+ format;
            }

            if(body != null && s.length() > 0 ){
              Attachment att = new Attachment();
              att.Name = attName;
              att.Body = body;
              att.ParentId = woId;
              insert att;
            }
        	
            
          	//System.debug(body.toString());
            integer j= 0;
            for(Attachment at :[select Id,Name from Attachment where ParentId = :woId]){
                 if(String.valueOf(at.Name).contains('安装')){
                 	j++;
                 }
           	}
           w.ReportNumber__c = j;

           if(w.Case__r.Status != '关闭')update w;
           msg = '保存成功!';
           isSuccess = true;/**/
        }catch(Exception e){
          Database.rollback(sp);
          msg = '保存失败'+e.getMessage();
         //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
        }
        
    }
}